package org.NooLab.itexx.app.controller.parser;

import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import org.NooLab.itexx.stuctures.UrlDescriptor;
import org.NooLab.itexx.stuctures.UrlDescriptors;
import org.NooLab.utilities.url.URLCanonicalizer;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


/*
   finding all links that belong to a certain class
      Elements links = doc.select("a.classname");

 */
/**
 * 
 * 
 * 
 * 
 *
 */
public class LinksExtractor {

	Document soupdoc;
	HTMLParser htmlParser;
	
	ArrayList<String> links = new ArrayList<String>();
	UrlDescriptors urlDescriptors = new UrlDescriptors();
	int urlCount;
	int MAX_OUT_LINKS =100;
	
	
	// ========================================================================
	public LinksExtractor(HTMLParser htmlParser, Document soupdoc) {
		//
		this.soupdoc = soupdoc;
		this.htmlParser = htmlParser;
		MAX_OUT_LINKS = htmlParser.MAX_OUT_LINKS ; 
	}
	// ========================================================================
	
	
	public void parse(String contextUrl) {

		String title,link;
		URL url;
		UrlDescriptor urlD;
		Set<String> _urlset = new HashSet<String>();
		
		Elements jsLinks = soupdoc.select("a[href]");

		
		for (Element e : jsLinks) {
			title = e.text();
			link  = e.attr("href");  
			
			// url = URLCanonicalizer.getCanonicalURL(link, contextUrl);
			// String _urlstr = url.toExternalForm();
			
			if (_urlset.contains(link)==false){

				_urlset.add(link);
				
				urlD = new UrlDescriptor();
				urlDescriptors.add(urlD) ;
				
				urlD.setLinkUrlStr(link);
				urlD.setDocUrlStr(contextUrl) ;
				
				int[] ps = htmlParser.strgutil.indexesAllOfStrings(htmlParser.htmlStr,new String[]{"=\""+link+"\""},0, true) ; 
				urlD.setPositionsInDoc( ps ) ;
				
				urlCount++;
				if (urlCount > MAX_OUT_LINKS) {
					break;
				}	
			}// not yet known?
			
		}// all found link elements
		title="";
	}
 
	
	public ArrayList<String> getLinks() {
		//
		return links;
	}


	public UrlDescriptors getRichUrls() {
		// 
		return urlDescriptors;
	}

}
